const koa = require('koa');
const Router = require('koa-router');
const mongoose = require('mongoose');
const bodyParser = require('koa-bodyparser');
const passport = require('koa-passport')

//实例化koa
const app = new koa();
const router = new Router();
app.use(bodyParser());




//引入users.js
const users = require('./routes/api/users.js');



//路由
router.get('/', async ctx => {
    ctx.body = { msg: 'HELLO KOA INTER' }
})

//config
const db = require('./config/keys.js').url;

//连接数据库
mongoose.connect(db,
    { useNewUrlParser: true })
    .then(() => {
        console.log('mongoose Connected..');
    })
    .catch(err => {
        console.log(err)
    })


app.use(passport.initialize());
app.use(passport.session());

//回调到config文件中 passport.js
require('./config/passport')(passport);

//配置路由地址
router.use('/api/users', users)

//配置路由
app.use(router.routes()).use(router.allowedMethods());


const post = process.env.PORT || 5000;
app.listen(post, () => {
    console.log(`sever started on ${post}`)
})
